package com.lmxdawn.api.dnf.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    public static final String GENERATEORDER_EXCHANGE="generateOrder_exchange";

    public static final String ORDER_PAY="order_pay";

    public static final String ORDER_TIMEOUT_QUEUE="order_timeout_queue";
    public static final String ORDER_TIMEOUT_EXCHANGE="order_timeout_exchange";


    public static final String ORDER_CREATE_QUEUE="order_create_queue";

    @Bean
    public Queue ORDER_CREATE_QUEUE(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-message-ttl",1000*60);
        arguments.put("x-dead-letter-exchange",ORDER_TIMEOUT_EXCHANGE);
        return new Queue(ORDER_CREATE_QUEUE,true,false,false,arguments);
    }


    @Bean(ORDER_TIMEOUT_EXCHANGE)
    public Exchange ORDER_TIMEOUT_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(ORDER_TIMEOUT_EXCHANGE).build();
    }

    @Bean(ORDER_TIMEOUT_QUEUE)
    public Queue ORDER_TIMEOUT_QUEUE(){
        return new Queue(ORDER_TIMEOUT_QUEUE);
    }

    @Bean
    public Binding ORDER_TIMEOUT_BINDING(@Qualifier(ORDER_TIMEOUT_QUEUE)Queue queue,@Qualifier(ORDER_TIMEOUT_EXCHANGE)Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }


    @Bean
    public Queue queue(){
        return  new Queue(ORDER_PAY);
    }


    /**
     * 声明商品订单交换机,采用路由模式
     * fanout:发布订阅
     * direct:路由模式
     * topic:通配符模式
     * headers:转发器模式
     * @return
     */
    @Bean(GENERATEORDER_EXCHANGE)
    public Exchange generateOrder_exchange(){
        return ExchangeBuilder.directExchange(GENERATEORDER_EXCHANGE).durable(true).build();
    }


}